package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.data.RobotRoomLogMapper;
import io.xxx.xbutler.data.RobotRoomMapper;
import io.xxx.xbutler.data.RoomMapper;
import io.xxx.xbutler.domain.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;

/**
 * 机器人主动退群回调
 *
 * <pre>
 * {
 *     "nType": 4007,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcChatRoomId": "",
 *         "vcChatRoomSerialNo": ""
 *     }
 * }
 * 或
 * {
 *     "Data": {},
 *     "nType": 4007,
 *     "nResult": 4502,
 *     "vcResult": "机器人不在群内",
 *     "nTimeStamp": 1740468104072,
 *     "vcSerialNo": "20250225152143979012927265802",
 *     "vcRobotWxId": "wxid_bysnjh4y1f1922",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": "DA1B048A0F72A1142FE1F29C982E3B79"
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4007Handler implements CallbackHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Autowired
    private RobotMapper robotMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private RobotRoomMapper robotRoomMapper;

    @Resource
    private RobotRoomLogMapper robotRoomLogMapper;

    @Override
    public String name() {
        return "机器人主动退群回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4007;
    }

    @Override
    public void handle(Callback callback) {
        if (!callback.isSuccess()) {
            return;
        }

        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();
        String robotId = callback.getRobotId();
        String robotSerialNo = callback.getRobotSerialNo();
        String roomId = data.getString("vcChatRoomId");
        String roomSerialNo = data.getString("vcChatRoomSerialNo");

        Robot robot = robotMapper.selectById(robotId);
        Room room = roomMapper.selectById(roomId);

        LambdaQueryWrapper<RobotRoom> wrapper = new LambdaQueryWrapper<RobotRoom>()
                .eq(RobotRoom::getRobotId, callback.getRobotId())
                .eq(RobotRoom::getRoomId, roomId);
        RobotRoomLog robotRoomLog = new RobotRoomLog()
                .setId(idGenerator.next())
                .setRobotId(robotId)
                .setRobotSerialNo(robotSerialNo)
                .setRoomId(roomId)
                .setRoomSerialNo(roomSerialNo)
                .setType(20)
                .setLeaveCode("robotLeaveRoom")
                .setLeaveReason("机器人主动退群")
                .setTime(callback.getTimestamp())
                .setCreatedTime(now);
        if (robot != null) {
            robotRoomLog.setRobotName(robot.getName())
                    .setBase64RobotName(robot.getBase64Name());
        }
        if (room != null) {
            robotRoomLog.setRoomName(room.getName())
                    .setBase64RoomName(room.getBase64Name())
                    .setRoomType(room.getType());
        }
        transactionTemplate.executeWithoutResult(_ -> {
            roomMapper.deleteById(roomId);
            robotRoomMapper.delete(wrapper);
            robotRoomLogMapper.insert(robotRoomLog);
        });
    }
}
